package sz.szsmk.citizencard.widget;
 
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardResponse;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

public class CardFunctionView extends FrameLayout implements View.OnClickListener {

    //    单个服务的item项
    private QueryUserCardResponse.DataBean itemBean;

    private ImageView service_icon;
    private TextView title, sub_title, action;
    private RelativeLayout status_open_layout, status_closer_layout;

    private TextView status_btn, card_tyhm_times, card_tyhm_endtimes,tv_sycs;
    private TextView tip_can_go, tip_hotel, tip_search, tip_history;
    private TextView btn_open_service, txt_desc;
    private String access_token;
    private LinearLayout footRl;

    public CardFunctionView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public CardFunctionView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CardFunctionView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 设置card类型
     *
     * @param itemBean
     */
    public void setItemBean(QueryUserCardResponse.DataBean itemBean) {
        this.itemBean = itemBean;
        upDateUI();
    }

    private void upDateUI() {
        title.setText(itemBean.getFuncName());

        if (  !TextUtils.isEmpty( itemBean.getFuncCode()) && ( itemBean.getFuncCode().equals("01") || itemBean.getFuncCode().equals("02")
                || itemBean.getFuncCode().equals("03") || itemBean.getFuncCode().equals("08")  ))  {

            switch (itemBean.getFuncCode()) {
                case "01":
                    service_icon.setImageResource(R.drawable.home_grid_xxnk);
                    break;
                case "02":
                    service_icon.setImageResource(R.drawable.home_grid_ylnk);
                    break;
                case "03":
                    service_icon.setImageResource(R.drawable.card_qzwwk);   //蓝色那个icon
                    break;
                case "08":
                    service_icon.setImageResource(R.drawable.home_grid_zzk);
                    break;
            }

            if (itemBean.getCardState().equals("0")) {
                status_closer_layout.setVisibility(VISIBLE);
                status_open_layout.setVisibility(GONE);
                action.setTextColor(0xff777777);
                String stateName = itemBean.getStateName();
                if (stateName != null) {
                    action.setText(itemBean.getStateName());
                }
                btn_open_service.setText("立即办理");
            } else {
                status_closer_layout.setVisibility(GONE);
                status_open_layout.setVisibility(VISIBLE);
                action.setTextColor(0xff00aaff);
                action.setText("更多详情");

                status_btn.setText(itemBean.getStateName());
//                btn_open_service.setVisibility(VISIBLE);

                tv_sycs.setVisibility(View.VISIBLE);
                card_tyhm_times.setVisibility(View.VISIBLE);

                card_tyhm_times.setText(itemBean.getSpareTimes() );

                card_tyhm_endtimes.setText(itemBean.getEndTime());

                if(!TextUtils.isEmpty(itemBean.getLeftContent())){
                    card_tyhm_times.setText(itemBean.getLeftContent());
                }

                switch (itemBean.getFuncCode()) {
                    case "01":
                        tip_can_go.setVisibility(VISIBLE);
                        tip_hotel.setVisibility(GONE);
                        tip_search.setVisibility(VISIBLE);
                        tip_history.setVisibility(GONE);
                        break;
                    case "02":
                        tip_can_go.setVisibility(GONE);
                        tip_hotel.setVisibility(GONE);
                        tip_search.setVisibility(GONE);
                        tip_history.setVisibility(GONE);
                        break;
                    case "03":
                        tip_can_go.setVisibility(VISIBLE);
                        tip_hotel.setVisibility(GONE);
                        tip_search.setVisibility(VISIBLE);
                        tip_history.setVisibility(GONE);
                        break;
                    case "08":
                        tip_can_go.setVisibility(VISIBLE);
                        tip_hotel.setVisibility(GONE);
                        tip_search.setVisibility(GONE);
                        tip_history.setVisibility(VISIBLE);
                        break;
                }

                if ("已过期".equals(itemBean.getStateName())) {
                    status_closer_layout.setVisibility(VISIBLE);
                    btn_open_service.setText("再次办理");
                    tip_can_go.setVisibility(GONE);
                    tip_hotel.setVisibility(GONE);
                    tip_search.setVisibility(GONE);
                    tip_history.setVisibility(GONE);
                }
            }
        } else {
            status_closer_layout.setVisibility(GONE);
            status_open_layout.setVisibility(GONE);

            if ("05".equals(itemBean.getFuncCode())) {
                service_icon.setImageResource(R.drawable.card_tsg);
                action.setTextColor(0xff777777);
                action.setText(itemBean.getStateName());
            }
        }

    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_card_function_view, this, true);
        access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        service_icon = (ImageView) findViewById(R.id.service_icon);
        title = (TextView) findViewById(R.id.title);
        footRl = (LinearLayout) findViewById(R.id.footprint_rl);
        sub_title = (TextView) findViewById(R.id.sub_title);
        action = (TextView) findViewById(R.id.action);
        action.setOnClickListener(this);

        status_open_layout = (RelativeLayout) findViewById(R.id.status_open_layout);
        status_btn = (TextView) findViewById(R.id.status_btn);
        card_tyhm_times = (TextView) findViewById(R.id.card_tyhm_times);
        card_tyhm_endtimes = (TextView) findViewById(R.id.card_tyhm_endtimes);

        tv_sycs = findViewById(R.id.tv_sycs);

        tip_can_go = (TextView) findViewById(R.id.tip_can_go);
        tip_hotel = (TextView) findViewById(R.id.tip_hotel);
        tip_search = (TextView) findViewById(R.id.tip_search);
        tip_history = (TextView) findViewById(R.id.tip_history);

        tip_can_go.setOnClickListener(this);
        tip_hotel.setOnClickListener(this);
        tip_search.setOnClickListener(this);
        tip_history.setOnClickListener(this);


        status_closer_layout = (RelativeLayout) findViewById(R.id.status_closer_layout);
        btn_open_service = (TextView) findViewById(R.id.btn_open_service);
        btn_open_service.setOnClickListener(this);
        txt_desc = (TextView) findViewById(R.id.txt_desc);
    }

    @Override
    public void onClick(View view) {
        access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");

        String webUrl = "";
        Bundle bundle = new Bundle();

        int id = view.getId();
        if (id == R.id.action) {//                查看详情可用时
            if (!itemBean.getCardState().equals("0") && (itemBean.getFuncCode().equals("01") || itemBean.getFuncCode().equals("02")
                    || itemBean.getFuncCode().equals("03") || itemBean.getFuncCode().equals("08"))) {
                if (itemBean.getFuncCode().equals("02")) {
                    webUrl = Constant.BASE_URL + "app-front-end/#/gardenCard/main?access_token=" + access_token;
                    bundle.putString("url", webUrl);
                    JumpUtils.jumptoNatvie(DaggerApplication.getIns(), "webView", 0, bundle);
                } else if (itemBean.getFuncCode().equals("01")) {
                    webUrl = Constant.BASE_URL + "app-front-end/#/card/main?access_token=" + access_token;
                    bundle.putString("url", webUrl);
                    JumpUtils.jumptoNatvie(DaggerApplication.getIns(), "webView", 0, bundle);

                } else if (itemBean.getFuncCode().equals("03")) {
                    webUrl = Constant.BASE_URL + "app-front-end/#/familycard/main";
                    bundle.putString("url", webUrl);
                    JumpUtils.jumptoNatvie(DaggerApplication.getIns(), "webView", 0, bundle);

                } else if (itemBean.getFuncCode().equals("05")) {
                    Toast.makeText(DaggerApplication.getIns(), "图 书馆", Toast.LENGTH_SHORT).show();

                } else if (itemBean.getFuncCode().equals("06")) {
                    Toast.makeText(DaggerApplication.getIns(), "吴江旅游年卡", Toast.LENGTH_SHORT).show();

                } else if (itemBean.getFuncCode().equals("07")) {
                    Toast.makeText(DaggerApplication.getIns(), "月票", Toast.LENGTH_SHORT).show();

                }
            }
        } else if (id == R.id.btn_open_service) {
            if (itemBean.getFuncCode().equals("02")) {
                webUrl = Constant.BASE_URL + "app-front-end/#/gardenCard/main?access_token=" + access_token;
                bundle.putString("url", webUrl);
                JumpUtils.jumptoNatvie(DaggerApplication.getIns(), "webView", 0, bundle);
            } else if (itemBean.getFuncCode().equals("01")) {
                webUrl = Constant.BASE_URL + "app-front-end/#/card/main?access_token=" + access_token;
                bundle.putString("url", webUrl);
                JumpUtils.jumptoNatvie(DaggerApplication.getIns(), "webView", 0, bundle);

            } else if (itemBean.getFuncCode().equals("03")) {
                webUrl = Constant.BASE_URL + "app-front-end/#/familycard/main?access_token=" + access_token;
                bundle.putString("url", webUrl);
                JumpUtils.jumptoNatvie(DaggerApplication.getIns(), "webView", 0, bundle);

            } else if (itemBean.getFuncCode().equals("05")) {
                Toast.makeText(DaggerApplication.getIns(), "图书馆", Toast.LENGTH_SHORT).show();

            } else if (itemBean.getFuncCode().equals("06")) {
                Toast.makeText(DaggerApplication.getIns(), "吴江旅游年卡", Toast.LENGTH_SHORT).show();

            } else if (itemBean.getFuncCode().equals("07")) {
                Toast.makeText(DaggerApplication.getIns(), "月票", Toast.LENGTH_SHORT).show();

            }

//                四个底部小选项
        } else if (id == R.id.tip_can_go) {
            if (itemBean.getFuncCode().equals("03")) {
                webUrl = Constant.BASE_URL + "app-front-end/#/familyCard/availableScenerySpot?isMain=1";
                bundle.putString("url", webUrl);
                bundle.putString(Constant.TITLE, "");
            } else {
                webUrl = Constant.BASE_URL + "app-front-end/#/card/availableScenerySpot?access_token=" + access_token + "&isMain=1" + "&cardNo=" + itemBean.getBindCardNo();
                L.e(Constant.TAG, "可取景点url----" + webUrl);
                bundle.putString("url", webUrl);
            }

            JumpUtils.jumptoNatvie(DaggerApplication.getIns(), "webView", 0, bundle);
        } else if (id == R.id.tip_hotel) {
            webUrl = Constant.BASE_URL + "app-front-end/#/card/availableHotel?access_token=" + access_token + "&isMain=1";
            bundle.putString("url", webUrl);
            JumpUtils.jumptoNatvie(DaggerApplication.getIns(), "webView", 0, bundle);
        } else if (id == R.id.tip_search) {
            if (itemBean.getFuncCode().equals("03")) {
                webUrl = Constant.BASE_URL + "app-front-end/#/familyCard/queryScenerySpotBook?access_token=" + access_token + "&isMain=1";
            } else {
                webUrl = Constant.BASE_URL + "app-front-end/#/card/queryScenerySpotBook?access_token=" + access_token + "&isMain=1";
            }
            bundle.putString("url", webUrl);
            JumpUtils.jumptoNatvie(DaggerApplication.getIns(), "webView", 0, bundle);
        } else if (id == R.id.tip_history) {
            webUrl = Constant.BASE_URL + "app-front-end/#/familycard/main";
            bundle.putString("url", webUrl);
            JumpUtils.jumptoNatvie(DaggerApplication.getIns(), "webView", 0, bundle);
        }
    }
}
